﻿using Abp;
using Abp.Runtime.Session;
using Abp.Threading;
using AbpProjectTemplate.Domain.Users.Admins;
using AbpProjectTemplate.Domain.Users.Members;
using AbpProjectTemplate.Domain.Users;
using System.Threading.Tasks;

namespace AbpProjectTemplate.Components.Runtime
{
    public static class AbpSessionExtensions
    {
        public static UserType? GetUserType(this IAbpSession abpSession)
        {
            return abpSession.GetUser()?.UserType;
        }
        /// <summary>
        /// 获取登录用户信息
        /// </summary>
        public static async Task<User> GetUserAsync(this IAbpSession abpSession)
        {
            var session = abpSession as AppSession;
            return await session.GetUserAsync();
        }
        /// <summary>
        /// 获取登录用户信息
        /// </summary>
        public static User GetUser(this IAbpSession abpSession)
        {
            var session = abpSession as AppSession;
            return session.GetUser();
        }

        /// <summary>
        /// 获取登录管理员信息
        /// </summary>
        public static async Task<Admin> GetAdminAsync(this IAbpSession abpSession)
        {
            return (await abpSession.GetUserAsync()) as Admin;
        }
        public static async Task<bool> IsAdminAsync(this IAbpSession abpSession)
        {
            var user = await abpSession.GetUserAsync();
            return user != null && user.UserType == UserType.Admin;
        }
        public static bool IsAdmin(this IAbpSession abpSession)
        {
            return AsyncHelper.RunSync(() => abpSession.IsAdminAsync());
        }

        /// <summary>
        /// 获取登录会员信息
        /// </summary>
        public static async Task<Member> GetMemberAsync(this IAbpSession abpSession)
        {
            return (await abpSession.GetUserAsync()) as Member;
        }
        public static async Task<bool> IsMemberAsync(this IAbpSession abpSession)
        {
            var member = await abpSession.GetMemberAsync();
            return member != null;
        }
        public static bool IsMember(this IAbpSession abpSession)
        {
            return AsyncHelper.RunSync(() => abpSession.IsMemberAsync());
        }


    }
}

